<template>
  <div class="refund-list">
    <div class="page-header">
      <h2>退款记录列表</h2>
      <el-button type="primary" @click="$router.push('/refund/create')">
        申请退款
      </el-button>
    </div>

    <el-form :inline="true" :model="queryForm" class="query-form">
      <el-form-item label="退款单号">
        <el-input v-model="queryForm.refundNo" placeholder="请输入退款单号" />
      </el-form-item>
      <el-form-item label="订单号">
        <el-input v-model="queryForm.orderNo" placeholder="请输入订单号" />
      </el-form-item>
      <el-form-item label="退款状态">
        <el-select v-model="queryForm.status" placeholder="请选择状态">
          <el-option label="全部" :value="null" />
          <el-option label="待处理" :value="0" />
          <el-option label="退款成功" :value="1" />
          <el-option label="退款失败" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="refundNo" label="退款单号" width="180" />
      <el-table-column prop="orderNo" label="订单号" width="180" />
      <el-table-column prop="amount" label="退款金额" width="120">
        <template #default="{ row }">
          ¥{{ row.amount }}
        </template>
      </el-table-column>
      <el-table-column prop="reason" label="退款原因" width="200" />
      <el-table-column prop="status" label="状态" width="120">
        <template #default="{ row }">
          <el-tag :type="getStatusType(row.status)">
            {{ getStatusText(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="申请时间" width="180" />
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button
            type="info"
            size="small"
            @click="handleDetail(row)"
          >
            详情
          </el-button>
          <el-button
            v-if="row.status === 0"
            type="success"
            size="small"
            @click="handleApprove(row)"
          >
            同意退款
          </el-button>
          <el-button
            v-if="row.status === 0"
            type="danger"
            size="small"
            @click="handleReject(row)"
          >
            拒绝退款
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'

const router = useRouter()
const tableData = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

const queryForm = ref({
  refundNo: '',
  orderNo: '',
  status: null
})

const getStatusType = (status) => {
  const types = {
    0: 'warning',
    1: 'success',
    2: 'danger'
  }
  return types[status] || 'info'
}

const getStatusText = (status) => {
  const texts = {
    0: '待处理',
    1: '退款成功',
    2: '退款失败'
  }
  return texts[status] || '未知'
}

const fetchData = async () => {
  try {
    const response = await axios.get('/api/refund/query', {
      params: {
        ...queryForm.value,
        page: currentPage.value,
        size: pageSize.value
      }
    })
    tableData.value = response.data.records
    total.value = response.data.total
  } catch (error) {
    ElMessage.error('获取数据失败')
  }
}

const handleQuery = () => {
  currentPage.value = 1
  fetchData()
}

const resetQuery = () => {
  queryForm.value = {
    refundNo: '',
    orderNo: '',
    status: null
  }
  handleQuery()
}

const handleSizeChange = (val) => {
  pageSize.value = val
  fetchData()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchData()
}

const handleDetail = (row) => {
  router.push(`/refund/detail/${row.refundNo}`)
}

const handleApprove = async (row) => {
  try {
    await ElMessageBox.confirm('确认同意该退款申请吗？', '提示', {
      type: 'warning'
    })
    await axios.post(`/api/refund/approve/${row.refundNo}`)
    ElMessage.success('已同意退款申请')
    fetchData()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('操作失败')
    }
  }
}

const handleReject = async (row) => {
  try {
    await ElMessageBox.confirm('确认拒绝该退款申请吗？', '提示', {
      type: 'warning'
    })
    await axios.post(`/api/refund/reject/${row.refundNo}`)
    ElMessage.success('已拒绝退款申请')
    fetchData()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('操作失败')
    }
  }
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.refund-list {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.query-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 